<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Models\Goods;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use App\Http\Models\UserAuth;
use App\Http\Models\UserSubmitAudit;
use App\Http\Models\User;
use App\Http\Models\GoodsCorrection;
use App\Http\Models\StatisticsGoodsInfo;
use App\Http\Models\UserCapital;
use App\Http\Models\StatisticsUser;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\Cache;

class HomeController extends Controller
{
    use ModelForm;
    protected $is_show = false;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('管理后台');

            // 选填
            $content->description('首页');
            $content->row(function ($row) {
                $row->column(2, '<h3><a href="/" target="_blank">若梦TV首页</a></h3>');
            });
            $content->row(function ($row) {
                $row->column(2, '<h3>用户统计</h3>');
            });
            $content->row(function ($row) {
                if ($this->is_show) {
                    $user_result = StatisticsUser::first();
                    $row->column(2, new InfoBox('注册用户', 'users', 'aqua', '/admin/user', $user_result ? $user_result->all_number : 0));
                    $row->column(2, new InfoBox('认证用户', 'users', 'aqua', '/admin/user/userauth?27cedf9624a7cb24076961ac8656d033=0', $user_result ? $user_result->is_auth : 0));
                    $row->column(2, new InfoBox('已发布商品用户', 'users', 'aqua', '/admin/user?send_user=true', $user_result ? $user_result->order_user : 0));
                    $row->column(2, new InfoBox('放单权限待审核', 'users', 'aqua', '/admin/user/permission?state=0', $user_result ? $user_result->order_apply : 0));
                    $row->column(2, new InfoBox('积分扣完用户', 'users', 'aqua', '/admin/userintegral?c36fb3c542627042c057705081c75be7=2', $user_result ? $user_result->zreo_integral_user : 0));
                } else {
                    //$data = Cache::remember('userStatistics', env('AdminCaCheTime'), function () {
                        $data['user'] = User::count();
                        $data['sendGoodsUserNum'] = Goods::groupBy('userid')->count();
                        $data['userauth'] = UserAuth::where(['status' => 0])->count();
                        $data['permission'] = UserSubmitAudit::where(['state' => 0])->count();
                        $data['userintegral'] = UserCapital::where('integral', '<=', 0)->count();
                  /*      return $data;
                });*/
                    $row->column(2, new InfoBox('注册用户', 'users', 'aqua', '/admin/user', $data['user']));
                    $row->column(2, new InfoBox('认证用户', 'users', 'aqua', '/admin/user/userauth?is_status=0', $data['userauth']));
                    $row->column(2, new InfoBox('已发布商品用户', 'users', 'aqua', '/admin/user?send_user=true', $data['sendGoodsUserNum']));
                    $row->column(2, new InfoBox('放单权限待审核', 'users', 'aqua', '/admin/user/permission?state=0', $data['permission']));
                    $row->column(2, new InfoBox('积分扣完用户', 'users', 'aqua', '/admin/userintegral?c36fb3c542627042c057705081c75be7=2', $data['userintegral']));
                }
            });
            $content->row(function ($row) {
                $row->column(2, '<h3>平推商品</h3>');
            });
            $content->row(function ($row) {
                if ($this->is_show) {
                    $goods = StatisticsGoodsInfo::where(['type' => 0])->orderBy('date', 'desc')->first();
                    $row->column(2, new InfoBox('在线商品', 'shopping-bag', 'green', '/admin/goods/unaudited?type=all_allow', $goods ? $goods->online : 0));
                    $row->column(2, new InfoBox('所有待审核', 'shopping-bag', 'green', '/admin/goods/unaudited', $goods ? $goods->all_unaudited : 0));
                    $row->column(2, new InfoBox('今日待审核', 'shopping-bag', 'green', '/admin/goods/unaudited?today=true', $goods ? $goods->today_unaudited : 0));
                    $row->column(2, new InfoBox('今日通过', 'shopping-bag', 'green', '/admin/goods/unaudited?type=all_allow&today=true', $goods ? $goods->today_adopt : 0));
                    $row->column(2, new InfoBox('今日拒绝', 'shopping-bag', 'green', '/admin/goods/unaudited?type=refuse&today=true', $goods ? $goods->today_refuse : 0));
                } else {
                    //$data = Cache::remember('statisticsGoodsOrdinary', env('AdminCaCheTime'), function () {
                        $data['online'] = Goods::where(['goodstype' => 0, 'goodsstate' => 2])->count();
                        $data['all_unaudited'] = Goods::where(['goodstype' => 0, 'goodsstate' => 0])->count();
                        $data['today_unaudited'] = Goods::where(['goodstype' => 0, 'goodsstate' => 0])->whereDate('created_at', date('Y-m-d'))->count();
                        $data['today_adopt'] = Goods::where(['goodstype' => 0, 'goodsstate' => 2])->whereDate('status_time', date('Y-m-d'))->where('userid','>=',10000)->count();
                        $data['today_refuse'] = Goods::where(['goodstype' => 0, 'goodsstate' => 3])->whereDate('status_time', date('Y-m-d'))->where('userid','>=',10000)->count();
                      //  return $data;
                    //});
                    $row->column(2, new InfoBox('在线商品', 'shopping-bag', 'green', '/admin/goods/unaudited?type=all_allow', $data['online']));
                    $row->column(2, new InfoBox('所有待审核', 'shopping-bag', 'green', '/admin/goods/unaudited', $data['all_unaudited']));
                    $row->column(2, new InfoBox('今日待审核', 'shopping-bag', 'green', '/admin/goods/unaudited?today=true', $data['today_unaudited']));
                    $row->column(2, new InfoBox('今日通过', 'shopping-bag', 'green', '/admin/goods/unaudited?type=all_allow&today=true', $data['today_adopt']));
                    $row->column(2, new InfoBox('今日拒绝', 'shopping-bag', 'green', '/admin/goods/unaudited?type=refuse&today=true', $data['today_refuse']));
                }
            });
            $content->row(function ($row) {
                $row->column(2, '<h3>爆款商品</h3>');
            });
            $content->row(function ($row) {
                if ($this->is_show) {
                    $goods = StatisticsGoodsInfo::where(['type' => 1])->orderBy('date', 'desc')->first();
                    $row->column(2, new InfoBox('在线商品', 'comment', 'maroon', '/admin/bursting/examine?type=all_allow', $goods ? $goods->online : 0));
                    $row->column(2, new InfoBox('所有待审核', 'comment', 'maroon', '/admin/bursting/examine', $goods ? $goods->all_unaudited : 0));
                    $row->column(2, new InfoBox('今日待审核', 'comment', 'maroon', '/admin/bursting/examine?today=true', $goods ? $goods->today_unaudited : 0));
                    $row->column(2, new InfoBox('今日通过', 'comment', 'maroon', '/admin/bursting/examine?type=all_allow&today=true', $goods ? $goods->today_adopt : 0));
                    $row->column(2, new InfoBox('今日拒绝', 'comment', 'maroon', '/admin/bursting/examine?type=refuse&today=true', $goods ? $goods->today_refuse : 0));
                } else {
                    //$data = Cache::remember('statisticsGoodsBursting', env('AdminCaCheTime'), function () {
                        $data['online'] = Goods::where(['goodstype' => 1, 'goodsstate' => 2])->count();
                        $data['all_unaudited'] = Goods::where(['goodstype' => 1, 'goodsstate' => 0])->count();
                        $data['today_unaudited'] = Goods::where(['goodstype' => 1, 'goodsstate' => 0])->whereDate('created_at', date('Y-m-d'))->count();
                        $data['today_adopt'] = Goods::where(['goodstype' => 1, 'goodsstate' => 2])->whereDate('status_time', date('Y-m-d'))->where('userid','>=',10000)->count();
                        $data['today_refuse'] = Goods::where(['goodstype' => 1, 'goodsstate' => 3])->whereDate('status_time', date('Y-m-d'))->where('userid','>=',10000)->count();
                       // return $data;
                    //});
                    $row->column(2, new InfoBox('在线商品', 'comment', 'maroon', '/admin/bursting/examine?type=all_allow', $data['online']));
                    $row->column(2, new InfoBox('所有待审核', 'comment', 'maroon', '/admin/bursting/examine', $data['all_unaudited']));
                    $row->column(2, new InfoBox('今日待审核', 'comment', 'maroon', '/admin/bursting/examine?today=true', $data['today_unaudited']));
                    $row->column(2, new InfoBox('今日通过', 'comment', 'maroon', '/admin/bursting/examine?type=all_allow&today=true', $data['today_adopt']));
                    $row->column(2, new InfoBox('今日拒绝', 'comment', 'maroon', '/admin/bursting/examine?type=refuse&today=true', $data['today_refuse']));

                }

            });
            $content->row(function ($row) {
                $row->column(2, '<h3>秒杀商品</h3>');
            });
            $content->row(function ($row) {
                //$goods=StatisticsGoodsInfo::lastModel(2);
                if ($this->is_show) {
                    $goods = StatisticsGoodsInfo::where(['type' => 2])->orderBy('date', 'desc')->first();
                    $row->column(2, new InfoBox('在线商品', 'hourglass', 'purple', '/admin/kill/manage?type=all_allow', $goods ? $goods->online : 0));
                    $row->column(2, new InfoBox('所有待审核', 'hourglass', 'purple', '/admin/kill/manage', $goods ? $goods->all_unaudited : 0));
                    $row->column(2, new InfoBox('今日待审核', 'hourglass', 'purple', '/admin/kill/manage?t=true', $goods ? $goods->today_unaudited : 0));
                    $row->column(2, new InfoBox('今日通过', 'hourglass', 'purple', '/admin/kill/manage?type=all_allow&today=true', $goods ? $goods->today_adopt : 0));
                    $row->column(2, new InfoBox('今日拒绝', 'hourglass', 'purple', '/admin/kill/manage?type=refuse&today=true', $goods ? $goods->today_refuse : 0));
                } else {
                    //$data = Cache::remember('statisticsGoodsKill', env('AdminCaCheTime'), function () {
                        $data['online'] = Goods::where(['goodstype' => 2, 'goodsstate' => 2])->count();
                        $data['all_unaudited'] = Goods::where(['goodstype' => 2, 'goodsstate' => 0])->count();
                        $data['today_unaudited'] = Goods::where(['goodstype' => 2, 'goodsstate' => 0])->whereDate('created_at', date('Y-m-d'))->count();
                        $data['today_adopt'] = Goods::where(['goodstype' => 2, 'goodsstate' => 2])->whereDate('status_time', date('Y-m-d'))->where('userid','>=',10000)->count();
                        $data['today_refuse'] = Goods::where(['goodstype' => 2, 'goodsstate' => 3])->whereDate('status_time', date('Y-m-d'))->where('userid','>=',10000)->count();
                      //  return $data;
                    //});
                    $row->column(2, new InfoBox('在线商品', 'hourglass', 'purple', '/admin/kill/manage?type=all_allow', $data['online']));
                    $row->column(2, new InfoBox('所有待审核', 'hourglass', 'purple', '/admin/kill/manage', $data['all_unaudited']));
                    $row->column(2, new InfoBox('今日待审核', 'hourglass', 'purple', '/admin/kill/manage?t=true', $data['today_unaudited']));
                    $row->column(2, new InfoBox('今日通过', 'hourglass', 'purple', '/admin/kill/manage?type=all_allow&today=true', $data['today_adopt']));
                    $row->column(2, new InfoBox('今日拒绝', 'hourglass', 'purple', '/admin/kill/manage?type=refuse&today=true', $data['today_refuse']));
                }
            });
            $content->row(function ($row) {
                $row->column(2, '<h3>纠错统计</h3>');
            });
            $content->row(function ($row) {
                if ($this->is_show) {
                    //$error=StatisticsGoodsInfo::lastModel(3);
                    $error = StatisticsGoodsInfo::where(['type' => 3])->orderBy('date', 'desc')->first();
                    $row->column(2, new InfoBox('所有待纠错', 'times', 'red', '/admin/goodscorrection', $error ? $error->all_unaudited : 0));
                    $row->column(2, new InfoBox('今日待纠错', 'times', 'red', '/admin/goodscorrection?t=today_not', $error ? $error->today_unaudited : 0));
                    $row->column(2, new InfoBox('今日已纠错', 'times', 'red', '/admin/goodscorrection?t=already&today=true', $error ? $error->today_adopt : 0));
                } else {
                   // $data = Cache::remember('error', env('AdminCaCheTime'), function () {
                        $data['all_unaudited'] = GoodsCorrection::where(['state' => 0])->count();
                        $data['today_unaudited'] = GoodsCorrection::where(['state' => 0])->whereDate('created_at', date('Y-m-d'))->count();
                        $data['today_adopt'] = GoodsCorrection::where(['state' => 1])->whereDate('created_at', date('Y-m-d'))->count();
                     //   return $data;
                   // });
                    $row->column(2, new InfoBox('所有待纠错', 'times', 'red', '/admin/goodscorrection', $data['all_unaudited']));
                    $row->column(2, new InfoBox('今日待纠错', 'times', 'red', '/admin/goodscorrection?t=today_not', $data['today_unaudited']));
                    $row->column(2, new InfoBox('今日已纠错', 'times', 'red', '/admin/goodscorrection?t=already&today=true', $data['today_adopt']));
                }
            });
        });
    }
}
